package com.csy.controller;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ktt.bean.Bis_custom_user;
import com.ktt.service.Bis_custom_userService;
import com.ly.bean.Bis_beauty_config;
import com.ly.service.BeautyConfigService;
import com.sjh.service.Bag_service;
import com.sjh.service.ISjh_service;
import com.sjh.vo.Bis_beauty_record;
import com.sjh.vo.Bis_nurse_project;
import com.spc.service.Bis_opinionService;
import com.xbw.bean.Bis_Shop;
import com.xbw.mapper.Bis_cash_recordMapper;
import com.zsy.mapper.Bis_Shop_UserMapper;


@Controller
@RequestMapping("wechat")
public class ComprehensiveController {
	@Autowired
	ISjh_service service;
	
	@Autowired
	Bis_opinionService bis_opinionService;
	
	@Autowired
    Bis_Shop_UserMapper bis_Shop_UserMapper;
	//ktt
	@Autowired
	private Bis_custom_userService userService;
	
	@Autowired
    private Bis_cash_recordMapper bcr;
		
	@Autowired
	private BeautyConfigService service3;
	
	@Autowired
	Bag_service ser;
	
	
	//用来购买颜值币的页面
	@RequestMapping("/GobuyBeauty")
	public String GobuyBeauty(Bis_beauty_config config,Map<String, Object> map,Model model){
		List<Bis_beauty_config> list=service3.querybeautyconfig(config);
		model.addAttribute("list", list);
		return "/csy/wechat/my/buyBeauty";
	}
	
	//跳转到用来显示交易明细的页面
	@RequestMapping("/GobeautyRecordList")
	public String GobeautyRecordList(HttpSession session,Model model){
		Bis_custom_user ss = (Bis_custom_user) session.getAttribute("list");				
		String custom_user_id=ss.getCustom_user_id();
	System.out.println("custom_user_id  65:   "+custom_user_id);
		List<Bis_beauty_record> bis_beauty_record = ser.getBis_beauty_recordByid(custom_user_id);
	System.out.println("bis_beauty_record  66:   "+bis_beauty_record);
		model.addAttribute("bis_beauty_record", bis_beauty_record);
		return "/csy/wechat/my/beautyRecordList";
	}
	//将交易明细详细信息显示出来
		@RequestMapping("/Gobeauty")
		@ResponseBody
		public List<Bis_beauty_record> Gobeauty(HttpSession session){
			Bis_custom_user ss=(Bis_custom_user) session.getAttribute("list");
			String custom_user_id=ss.getCustom_user_id();
		System.out.println("custom_user_id  78:   "+custom_user_id);
			List<Bis_beauty_record> bis_beauty_record =ser.getBis_beauty_recordByid(custom_user_id);
		System.out.println("bis_beauty_record  79:   "+bis_beauty_record);
			return bis_beauty_record;
		}
		
	
	
	//跳转到用来兑换颜值币的界面
	@RequestMapping("/GoexchangeBeauty")
	public String GoexchangeBeauty(){
		return "/csy/wechat/my/exchangeBeauty";
	}
		
		
	
	//跳转到用来显示个人详细的链接
		@RequestMapping("/GomyDetail")
		public String GomyDetail(Model model,HttpSession session){
			Bis_custom_user ss=(Bis_custom_user) session.getAttribute("list");
			int id=Integer.parseInt(ss.getCustom_user_id());
			Bis_custom_user bis_custom_user=userService.getById(id);
			model.addAttribute("bis_custom_user", bis_custom_user);
			return "/csy/wechat/my/myDetail";
		}
		@RequestMapping("/updatemyDetail")
		@ResponseBody
		public String updatemyDetail(@RequestParam("custom_user_id")String custom_user_id,@RequestParam("nikename")String nikename,
				@RequestParam("born_date")String born_date,@RequestParam("sex")String sex){
			Map<String,Object> map=new HashMap<>();
			map.put("custom_user_id", custom_user_id);
			map.put("nikename", nikename);
			map.put("born_date", born_date);
			map.put("sex", sex);
			this.userService.updateuser(map);
			return "1";
		}
	
	//商品详情
	@RequestMapping("/GoshowProjectDetail")
	public String selectByid(String project_id,Model model){
		Bis_nurse_project list = service.selectByid(project_id);
		System.out.println("商品详情: "+list);
		model.addAttribute("project",list);
		return "/csy/wechat/project/showProjectDetail";
	}
	
	
	//跳转到用来预约商品信息的链接
	@RequestMapping("/GofindShop")
	public String GofindShop(@RequestParam(value="returnType")String returnType,@RequestParam(value="project_id")String project_id,Model model){
		if("1".equals(returnType)){
			model.addAttribute("project_id", project_id);
			return "/csy/wechat/project/selectShop";
		}else{
			model.addAttribute("project_id", project_id);
			return "/csy/wechat/shop/findShop";
		}
	}
	
	//跳转到店铺的信息的链接
	@RequestMapping("/GoselectShop")
	@ResponseBody
	public List<Bis_Shop> GoselectShop(Model model){
		List<Bis_Shop> bis_shop=bcr.selectBis_shop();
		System.out.println("跳转到店铺啊 :   142"+bis_shop);
		return bis_shop;
	}
	
	//从店铺跳转到商品信息的链接(预约)
	@RequestMapping("/GosubscribeProject")
	public String GosubscribeProject(Model model,@RequestParam(value="project_id")String project_id,@RequestParam(value="shop_id")String shop_id,HttpSession session){
		Bis_custom_user bis_custom_user=(Bis_custom_user) session.getAttribute("list");
		//显示页面
		Bis_nurse_project bis_nurse_project=ser.getBis_nurse_projectId(project_id);
		model.addAttribute("bis_nurse_project", bis_nurse_project);
		Bis_Shop bis_shop= bcr.getBis_shopByid(shop_id);
		model.addAttribute("bis_shop", bis_shop);
		model.addAttribute("bis_custom_user", bis_custom_user);
		return "/csy/wechat/project/subscribeProject";
	}
	
	/**
	 * 意见反馈
	 * @return
	 */
	//用来显示微信平台进入意见反馈
	@RequestMapping("/Gofeedback")
	public String Gofeedback(){
		return "/csy/wechat/my/feedback";
	}
	//用来意见反馈
	@RequestMapping("/Gofeed")
	@ResponseBody
	public String Gofeed(@RequestParam(value="content")String content,HttpSession session){
		//随机生成消息ID
		SimpleDateFormat sdf=new SimpleDateFormat("yyMMddHHmmss");
		String str=sdf.format(new Date());
		Random random=new Random();
		int num= random.nextInt(9999)+100;
		String id=str+num;
		//插入消息
		Bis_custom_user ss=(Bis_custom_user) session.getAttribute("list");
		Map<String, Object> map=new HashMap<>();
		map.put("opinion_id",id);
		map.put("custom_user_id", ss.getCustom_user_id());
		map.put("content", content);
		this.bis_opinionService.saveBis_opinion(map);
		System.out.println("我走了");
		return "1";
	}
	
	/**
	 * 商品预约支付
	 */
	@RequestMapping("/orderpay")
	@ResponseBody
	public String orderpay(@RequestParam("project_id")String project_id,@RequestParam("shop_id")String shop_id,
			@RequestParam("subscribe_time")String subscribe_time,@RequestParam("beauty_price")String beauty_price,
			@RequestParam("order_content")String order_content,HttpSession session){
		//生成5位随机数
		int max=999999;
        int min=10000;
        Random random = new Random();
        Integer ran = random.nextInt(max)%(max-min+1) + min;
        String code1=ran.toString();
        int max1=999;
        int min1=10;
        Random random1 = new Random();
        Integer ran1 = random1.nextInt(max1)%(max1-min1+1) + min1;
        String code2=ran1.toString();
        String code=code1+code2;
        String code3="1706"+code1;
		Bis_custom_user ss=(Bis_custom_user) session.getAttribute("list");
		Map<String, Object> map=new HashMap<>();
		map.put("custom_user_id", ss.getCustom_user_id());
		map.put("project_id", project_id);
		map.put("shop_id", shop_id);
		map.put("service_time", subscribe_time);//服务时间
		map.put("extend_beauty_num", beauty_price);//订单金额
		map.put("order_id", code);
		map.put("order_content", order_content);
		map.put("beauty_price", beauty_price);//用户扣金额
		//向预约白添加预约信息
		this.bis_opinionService.saveBis_bisiness_order(map);
		//修改用户信息的颜值币
		this.bis_opinionService.updateBis_custom_user(map);
		System.out.println("预约完成我走了!");
		return "1";
	}
}
